import { Router } from 'vue-router'
import { useProgress, ProgressFinisher } from '@marcoschulte/vue3-progress'

export function createPageLoadingGuard(router: Router) {
    let progresses: ProgressFinisher | null = null

    router.beforeEach(async () => {
        if (progresses !== null) {
            progresses.finish()
        }
        progresses = useProgress().start()
        return true
    })
    router.afterEach(async () => {
        progresses?.finish()
        progresses = null
        return true
    })
    router.onError(async () => {
        progresses?.finish()
        progresses = null
        return true
    })
}
